#include "../../tedi2lang/dessert.hpp"
#include "../../tedi2lang/tedi2lang.h"
#include "../tedi2md.h"
#include "../../tedi2lang/exception.h"
#include <string>
#include <cstring>
#include <iostream>
int main(){
	
	tedi2md td;

	//Empty string
	cout << "EMPTY STRINGS" << endl;

	dessert((td.convert("", "", "")) == (""));
	dessert(td.convert(" ", "", "") == (" \n"));
	dessert(td.convert("\n\n\n", "", "") == ("\n\n\n"));

	//Titles
	cout << "TITLES" << endl;

	dessert(throws(td.convert("#####", "", "")));
	dessert(td.convert("paragraph\n#This isn't a title", "", "") == ("paragraph\n# This isn't a title\n"));

	dessert(td.convert("#This isn't a title", "", "") == ("# This isn't a title\n"));
	dessert(td.convert(" #This isn't a title", "", "") == ("# This isn't a title\n"));
	dessert(td.convert("\t##This isn't a title", "", "") == ("## This isn't a title\n"));

	dessert(td.convert("##This isn't a title", "", "") == ("## This isn't a title\n"));
	dessert(td.convert("###This isn't a title", "", "") == ("### This isn't a title\n"));
	dessert(td.convert("####This isn't a title", "", "") == ("#### This isn't a title\n"));
	dessert(td.convert("#####This isn't a title", "", "") == "#### This isn't a title\n" );
	dessert(td.convert("#####This isn't a title", "", "") == "#### This isn't a title\n" );

	cout << "LIST" << endl;

	dessert(td.convert("__", "", "") == ("\n"));
	dessert(td.convert("--Text", "", "") == ("--Text\n"));
	dessert(td.convert(",,", "", "") == (",,\n"));
	dessert(td.convert("__\n--Text\n,,", "", "") == ("\n- Text\n\n"));

	cout << "QUOTE" << endl;

	dessert(td.convert("\"Quoted text\"", "", "") == ("Quoted text\n"));
	dessert(throws(td.convert("\"Bad quoting", "", "")));

	cout << "LINKS" << endl;

	dessert(td.convert("[Test with a open square bracket", "", "") == ("[Test with a open square bracket\n"));
	dessert(throws(td.convert("[(Test with a open square bracket and parenthesis", "", "")));
	dessert(throws(td.convert("[(Test without closing square bracket)", "", "")));
	dessert(throws(td.convert("[(Test with closing square bracket)]", "", "")));
	dessert(td.convert("[(Test with closing square bracket) ]", "", "") == "<a href=\"Test with closing square bracket\"></a>\n");

	dessert(td.convert("[() ]", "", "") == "<a href=\"\"></a>\n");
	dessert(td.convert("[( ) ]", "", "") == "<a href=\" \"></a>\n");

	dessert(throws(td.convert("[(test\\) test with escaped char\\) ]", "", "")));

	dessert(td.convert("[(test\\) test with escaped char) ]", "", "") == "<a href=\"test) test with escaped char\"></a>\n");
	dessert(td.convert("[(test\\) test with escaped char) \\]sasdfasd]", "", "") == "<a href=\"test) test with escaped char\">]sasdfasd</a>\n");
	dessert(td.convert("[(test\\( test with escaped char) \\]sasdfasd]", "", "") == "<a href=\"test( test with escaped char\">]sasdfasd</a>\n");

	dessert(td.convert("\\([Test with closing square bracket) text", "", "") == "([Test with closing square bracket) text\n");
	dessert(td.convert("\\([ ([Test with closing square bracket] text)", "", "") == "([ <img alt=\"Test with closing square bracket\" src=\"text\">\n");

	cout << "IMAGES" << endl;

	dessert(td.convert("(Test with an open parenthesis", "", "") == ("(Test with an open parenthesis\n"));
	
	dessert(throws(td.convert("([Test with a open square bracket and parenthesis", "", "")));
	dessert(throws(td.convert("([Test without closing square bracket]", "", "")));

	dessert(throws(td.convert("([Test with closing square bracket])", "", "")));
	dessert(td.convert("([Test with closing square bracket] )", "", "") == "<img alt=\"Test with closing square bracket\" src=\"\">\n");
	dessert(td.convert("([] )", "", "") == "<img alt=\"\" src=\"\">\n");
	dessert(td.convert("([ ] )", "", "") == "<img alt=\" \" src=\"\">\n");

	dessert(throws(td.convert("([test\\] test with escaped char\\] )", "", "")));
	dessert(td.convert("([test\\] test with escaped char] )", "", "") == "<img alt=\"test] test with escaped char\" src=\"\">\n");
	dessert(td.convert("([test\\] test with escaped char] \\)sasdfasd)", "", "") == "<img alt=\"test] test with escaped char\" src=\")sasdfasd\">\n");

	dessert(td.convert("\\([Test with closing square bracket) text", "", "") == "([Test with closing square bracket) text\n");
	dessert(td.convert("\\([ ([Test with closing square bracket] text)", "", "") == "([ <img alt=\"Test with closing square bracket\" src=\"text\">\n");

	cout << "BLOCKS" << endl;

	dessert(td.convert("{Test with an open parenthesis", "", "") == ("{Test with an open parenthesis\n"));
	dessert(throws(td.convert("{(Test with a open square bracket and parenthesis", "", "")));
	dessert(throws(td.convert("{(Test without closing square bracket)", "", "")));
	dessert(throws(td.convert("{(Test with closing square bracket)}", "", "")));
	dessert(td.convert("{(Test with closing square bracket) }", "", "") == "<div class=\"Test with closing square bracket\"></div>\n");
	dessert(td.convert("{() }", "", "") == "<div class=\"\"></div>\n");
	dessert(td.convert("{( ) }", "", "") == "<div class=\" \"></div>\n");

	dessert(throws(td.convert("{(test\\) test with escaped char\\) }", "", "")));
	dessert(td.convert("{(test\\) test with escaped char) }", "", "") == "<div class=\"test) test with escaped char\"></div>\n");
	dessert(td.convert("{(test\\) test with escaped char) \\}sasdfasd}", "", "") == "<div class=\"test) test with escaped char\">}sasdfasd</div>\n");

	dessert(td.convert("\\{(Test with closing square bracket) text", "", "") == "{(Test with closing square bracket) text\n");
	dessert(td.convert("\\{( {(Test with closing square bracket) text}", "", "") == "{( <div class=\"Test with closing square bracket\">text</div>\n");

	cout << "MULTIPLES UNQUOTED TAGS" << endl;

	dessert(td.convert("[Test with a open square bracket ( also parenthesis and { brackets", "", "") == ("[Test with a open square bracket ( also parenthesis and { brackets\n"));
	dessert(td.convert("[(url) Test with a link ([alternative text] image url)] and {(class) brackets}", "", "") == ("<a href=\"url\">Test with a link <img alt=\"alternative text\" src=\"image url\"></a> and <div class=\"class\">brackets</div>\n"));
	dessert(td.convert("{(class) [(url) Test with a link ([alternative text] image url)] and brackets}", "", "") == ("<div class=\"class\"><a href=\"url\">Test with a link <img alt=\"alternative text\" src=\"image url\"></a> and brackets</div>\n"));

	cout << "TABLE" << endl;

	dessert(td.convert("| first cell | second cell |", "", "") == "<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\n");
	dessert(td.convert("| first cell ", "", "") == "| first cell \n");

	dessert(throws(td.convert("| first cell | second cell ", "", "")));

	dessert(td.convert("| first cell | second cell |\n| third cell | fourth cell |", "", "") == "<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n");
	dessert(td.convert("| first cell |\n| second cell | third cell |", "", "") == "<table>\n<tr>\n<td> first cell </td>\n</tr>\n<tr>\n<td> second cell </td>\n<td> third cell </td>\n</tr>\n</table>\n");
	dessert(td.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |", "", "") == "<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n");
	dessert(td.convert("Text adhoc for testing\n| a | b |\n| c | d |\n", "", "") == "Text adhoc for testing\n<table>\n<tr>\n<td> a </td>\n<td> b </td>\n</tr>\n<tr>\n<td> c </td>\n<td> d </td>\n</tr>\n</table>\n");
	dessert(td.convert("Text adhoc for testing\n| a | b |\n| c | d |\nText adhoc for testing\n", "", "") == "Text adhoc for testing\n<table>\n<tr>\n<td> a </td>\n<td> b </td>\n</tr>\n<tr>\n<td> c </td>\n<td> d </td>\n</tr>\n</table>\nText adhoc for testing\n");

	dessert(td.convert(" {() #This isn't a title}", "", "") == (" <div class=\"\">#This isn't a title</div>\n"));
	dessert(td.convert(" {(class) #This isn't a title}", "", "") == (" <div class=\"class\">#This isn't a title</div>\n"));
	try{
	dessert(td.convert(" {(class) \n #This is a title \n }", "", "") == (" <div class=\"class\">\n# This is a title \n </div>\n"));
	}catch(Invalid i){ cout << i.line() << i.reason() << endl;}
	dessert(throws(td.convert(" {(class)#This isn't a title", "", "")));

	cout << "END TABLE" << endl;

	dessert(td.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n#Title1", "", "") == "<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n# Title1\n");
	dessert(td.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n__\n--Item\n,,", "", "") == "<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n\n- Item\n\n");
	dessert(td.convert("__\n| first cell | second cell |\nText line\n| third cell | fourth cell |\n--Item\n,,", "", "") == "\n<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n- Item\n\n");
	dessert(td.convert("__\n--Item\n| first cell | second cell |\nText line\n| third cell | fourth cell |\n,,", "", "") == "\n- Item\n<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n\n");
	dessert(td.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n<Test\n", "", "") == "<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\nTest\n");
	dessert(td.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n<+Test\n", "", "") == "<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\nTest\n");
	dessert(td.convert("| first cell | second cell |\nText line\n| third cell | fourth cell |\n<+Test with pipe |\n", "", "") == "<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\nTest with pipe |\n");

	cout << "NEW LINE" << endl;

	dessert(td.convert("Lorem Ipsum \n ", "", "") == ("Lorem Ipsum \n \n"));
	dessert(td.convert("First line\nLorem Ipsum \n ", "", "") == ("First line\nLorem Ipsum \n \n"));
	dessert(td.convert("First line \nLorem Ipsum \n ", "", "") == ("First line \nLorem Ipsum \n \n"));
	dessert(td.convert("Lorem{(class) Ipsum} \n", "", "") == ("Lorem<div class=\"class\">Ipsum</div> \n"));

	dessert(td.convert("Lorem\nIpsum\nDolor\n", "", "") == ("Lorem\nIpsum\nDolor\n"));
	dessert(td.convert("{(class) text}\n", "", "") == ("<div class=\"class\">text</div>\n"));
	dessert(td.convert("Lorem\n{(class) text}\nDolor\n", "", "") == ("Lorem\n<div class=\"class\">text</div>\nDolor\n"));

	cout << "METATAGS" << endl;

	dessert(td.convert("<! Comment commenting things", "", "") == "");

	dessert(td.convert("Text adhoc for testing\n< metatag without space at end\nText adhoc for testing", "", "") == "Text adhoc for testing\n metatag without space at end\nText adhoc for testing\n");
	dessert(td.convert("Text adhoc for testing \n< metatag without space at end\nText adhoc for testing", "", "") == "Text adhoc for testing \n metatag without space at end\nText adhoc for testing\n");
	dessert(td.convert("Text adhoc for testing\n< metatag without space at end\nText adhoc for testing ", "", "") == "Text adhoc for testing\n metatag without space at end\nText adhoc for testing \n");

	dessert(td.convert("Text adhoc for testing\n<+ metatag with space at end\nText adhoc for testing", "", "") == "Text adhoc for testing\n metatag with space at end\nText adhoc for testing\n");

	dessert(td.convert("Text adhoc for testing \n<+ metatag with space at end\nText adhoc for testing", "", "") == "Text adhoc for testing \n metatag with space at end\nText adhoc for testing\n");
	dessert(td.convert("Text adhoc for testing\n<+ metatag with space at end\nText adhoc for testing ", "", "") == "Text adhoc for testing\n metatag with space at end\nText adhoc for testing \n");

	dessert(td.convert("Text adhoc for testing\n<> embed with space at end\nText adhoc for testing ", "", "") == "Text adhoc for testing\n embed with space at end\nText adhoc for testing \n");

	cout << "MISC." << endl;

	dessert(td.convert("alkasdfasdf \n#This isn't a title\nksadjfañlskdfj", "", "")
		== ("alkasdfasdf \n# This isn't a title\nksadjfañlskdfj\n"));

	dessert(td.convert("alkasdfasdf\n#This isn't a title\nksadjfañlskdfj", "", "")
		 == "alkasdfasdf\n# This isn't a title\nksadjfañlskdfj\n");

	dessert(td.convert("Text adhoc for testing\n{(class) container text } Text adhoc for testing ", "", "") == "Text adhoc for testing\n<div class=\"class\">container text </div> Text adhoc for testing \n");
	dessert(td.convert("Text adhoc for testing \n{(class) container text }\nText adhoc for testing ", "", "") == "Text adhoc for testing \n<div class=\"class\">container text </div>\nText adhoc for testing \n");
	dessert(td.convert("Text adhoc for testing \n{(class) container text}Text adhoc for testing ", "", "") == "Text adhoc for testing \n<div class=\"class\">container text</div>Text adhoc for testing \n");

	dessert(td.convert("Paragraph without space at end\n< hello ", "", "") == "Paragraph without space at end\n hello \n");
	dessert(td.convert("Paragraph without space at end\n< hello without space at end\n lsdkjfgsñdlkfjñlk", "", "") == "Paragraph without space at end\n hello without space at end\n lsdkjfgsñdlkfjñlk\n");
	dessert(td.convert("Paragraph without space at end\n< hello ", "", "") == "Paragraph without space at end\n hello \n");
	
}
